<?php
//
// +--------------------------------------------------------------------+
// | ZF Skeleton Demo Application - by Onlime Webhosting                |
// +--------------------------------------------------------------------+
// | Copyright (c) 2007-2009 Onlime Webhosting, Philip Iezzi            |
// |                         http://www.onlime.ch/                      |
// +--------------------------------------------------------------------+
// | Powered by Zend Framework - http://framework.zend.com/             |
// +--------------------------------------------------------------------+
//
// Shiny exception template has been ported for Zend Framework 1.8.x
// from Agavi 1.0.1, http://www.agavi.org
// see original copyright notices below...


// +---------------------------------------------------------------------------+
// | This file is part of the Agavi package.                                   |
// | Copyright (c) 2005-2009 the Agavi Project.                                |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code. You can also view the    |
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
// |   vi: set noexpandtab:                                                    |
// |   Local Variables:                                                        |
// |   indent-tabs-mode: t                                                     |
// |   End:                                                                    |
// +---------------------------------------------------------------------------+

/**
 * A drop dead gorgeous exception template with eye candy embedded as inline SVG
 *
 * @package    agavi
 * @subpackage exception
 *
 * @author     David Zülke <dz@bitxtender.com>
 * @copyright  Authors
 * @copyright  The Agavi Project
 *
 * @since      0.11.0
 *
 * @version    $Id: shiny.php 3915 2009-03-11 16:09:57Z saracen $
 */

if(!function_exists('buildParamList')) {
	/**
	 * Build a list of parameters passed to a method. Example:
	 * array([object AgaviFilter], 'baz' => array(1, 2), 'log' => [resource stream])
	 *
	 * @param      array An (associative) array of variables.
	 *
	 * @return     string A string, possibly formatted using HTML "em" tags.
	 *
	 * @author     David Zülke <dz@bitxtender.com>
	 * @since      0.11.0
	 */
	function buildParamList($params)
	{
		$retval = array();
		foreach($params as $key => $param) {
			if(is_string($key)) {
				$key = htmlspecialchars(var_export($key, true) . ' => ');
			} else {
				$key = '';
			}
			switch(gettype($param)) {
				case 'array':
					$retval[] = $key . 'array(' . buildParamList($param) . ')';
					break;
				case 'object':
					$retval[] = $key . '[object <em>' . get_class($param) . '</em>]';
					break;
				case 'resource':
					$retval[] = $key . '[resource <em>' . htmlspecialchars(get_resource_type($param)) . '</em>]';
					break;
				case 'string':
					$retval[] = $key . htmlspecialchars(var_export(strlen($param) > 51 ? substr_replace($param, ' … ', 25, -25) : $param, true));
					break;
				default:
					$retval[] = $key . htmlspecialchars(var_export($param, true));
			}
		}
		return implode(', ', $retval);
	}
}

$e = $this->exception;

// we're not supposed to display errors
// let's throw the exception so it shows up in error logs
if(!ini_get('display_errors')) {
	throw $e;
}

$svg = false;
$ua = '';
if(isset($_SERVER['HTTP_USER_AGENT'])) {
	$ua = $_SERVER['HTTP_USER_AGENT'];
} elseif($container !== null && ($rd = $container->getRequestData()) !== null && $rd instanceof AgaviIHeadersRequestDataHolder && $rd->hasHeader('User-Agent')) {
	$ua = $rd->getHeader('User-Agent');
} elseif($context !== null && ($rq = $context->getRequest()) !== null && !$rq->isLocked() && ($rd = $rq->getRequestData()) !== null && $rd instanceof AgaviIHeadersRequestDataHolder) {
	$ua = $rd->getHeader('User-Agent');
}
if(strpos($ua, 'AppleWebKit') !== false) {
	if(preg_match('#AppleWebKit/(\d+)#', $ua, $matches)) {
		if((int)$matches[1] >= 420) {
			$svg = true;
		}
	}
} elseif(strpos($ua, 'Gecko') !== false) {
	if(preg_match('#rv:([0-9\.]+)#', $ua, $matches)) {
		if(version_compare($matches[1], '1.8', '>=')) {
			$svg = true;
		}
	}
}

header('HTTP/1.0 500 Internal Server Error');
if($svg) {
	header('Content-Type: application/xhtml+xml; charset=utf-8');
	echo '<?xml version="1.0" encoding="utf-8" standalone="no" ?>';
} else {
	header('Content-Type: text/html; charset=utf-8');
}

// fix stack trace in case it doesn't contain the exception origin as the first entry
$fixedTrace = $e->getTrace();
if(isset($fixedTrace[0]['file']) && !($fixedTrace[0]['file'] == $e->getFile() && $fixedTrace[0]['line'] == $e->getLine())) {
	$fixedTrace = array_merge(array(array('file' => $e->getFile(), 'line' => $e->getLine())), $fixedTrace);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"<?php if($svg): ?> xmlns:svg="http://www.w3.org/2000/svg"<?php endif; ?>>
	<head>
		<meta http-equiv="Content-Type" content="<?php if($svg): ?>application/xhtml+xml<?php else: ?>text/html<?php endif; ?>; charset=utf-8" />
		<title><?php echo get_class($e); ?></title>
		<meta http-equiv="Content-Language" content="en" />
		<meta name="robots" content="none" />
		<style type="text/css">
			html {
				background-color:		#EEE;
			}

			body {
				margin:							5em;
				padding:						2em;
				border:							1px solid #DDD;
				-moz-border-radius:	0.5em;
				background-color:		#FFF;
				font-family:				Verdana, Arial, sans-serif;
				line-height:				1.5em;
				font-size:					10pt;
			}

			h1 {
				margin:							0 0 1.5em 0;
			}

			h3 {
				margin:							2em 0 0 0;
			}

			.nice {
				margin:							1.5em 0 1.5em 1em;
				padding-left:				3.5em !important;
			}

			#message {
				font-weight:				bold;
				padding:						0.5em;
				-moz-border-radius:	0.5em;
				border:							1px solid #FB2;
				background-color:		#FFC;
				position:						relative;
			}

			#help {
				font-weight:				bold;
				padding:						0.5em;
				-moz-border-radius:	0.5em;
				border:							1px solid #66D;
				background-color:		#F0F0FF;
				position:						relative;
			}

			ol {
				font-size:					8pt;
				line-height:				1.5em;
			}

			li.hidecode ol {
				display:						none;
			}

			ol li {
				margin:							0 0 1em 0;
			}

			ol ol li {
				margin:							auto;
			}

			a.toggle:before {
				content:						'« ';
			}

			.hidecode a.toggle:before {
				content:						'';
			}

			.hidecode a.toggle:after {
				content:						' »';
			}

			ol ol {
				border:							1px solid #DDD;
				-moz-border-radius:	0.5em;
				font-family:				monospace;
				font-size:					10pt;
				line-height:				1em;
				min-height:					7em;
				padding-left:				auto;
				padding-top:				0.5em;
				padding-right:			0.5em;
				padding-bottom:			0.5em;
			}

			li.highlight code {
				background-color:		#EEE;
			}

			#svgDefinitions {
				width:							0;
				height:							0;
				overflow:						hidden;
			}

			abbr {
				border-bottom:			1px dotted #000;
				cursor:							help;
			}

			code {
				display:						block;
				margin:							0;
				padding:						0;
			}
<?php if($svg): ?>

			ol ol li {
				padding-left:				1em;
			}
<?php endif; ?>
		</style>
	</head>
	<body>
		
<?php if($svg): ?>
		<div style="float:right; margin:-6em -6em 0 0; width:10em; height:10em"><svg:svg viewBox="0 0 48 48" preserveAspectRatio="xMaxYMax meet" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><svg:use xlink:href="#exceptionSign" /></svg:svg></div>
<?php endif; ?>
		<h1><?php echo get_class($e); ?></h1>
<?php if($e instanceof Zend_Exception): ?>
		<p id="help"<?php if($svg): ?> class="nice"<?php endif; ?>>
<?php if($svg): ?>
			<div style="position:absolute; top:-1.25em; left:-2em; height:5em; width:5em;"><svg:svg viewBox="3 3 42 42" preserveAspectRatio="xMaxYMax meet" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><svg:use xlink:href="#helpSign" /></svg:svg></div>
<?php endif; ?>
			This is an internal Zend Framework exception. Please consult the documentation for assistance with solving this issue.</p>
<?php else: ?>
		<p id="help"<?php if($svg): ?> class="nice"<?php endif; ?>>
<?php if($svg): ?>
			<div style="position:absolute; top:-1.25em; left:-2em; height:5em; width:5em;"><svg:svg viewBox="3 3 42 42" preserveAspectRatio="xMaxYMax meet" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><svg:use xlink:href="#helpSign" /></svg:svg></div>
<?php endif; ?>
			This is <em>not</em> a Zend Framework exception, but likely an error that occurred in the application code.</p>
<?php endif; ?>
		<p>An exception of type <strong><?php echo get_class($e); ?></strong> was thrown, but did not get caught during the execution of the request. You will find information provided by the exception along with a stack trace below.</p>
<?php $msg = nl2br(htmlspecialchars($e->getMessage())); ?>
<?php if($msg != ''): ?>
		<p id="message"<?php if($svg): ?> class="nice"<?php endif; ?>>
<?php if($svg): ?>
			<div style="position:absolute; top:-1.25em; left:-2em; height:5em; width:5em;"><svg:svg viewBox="3 0 43 43" preserveAspectRatio="xMaxYMax meet" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><svg:use xlink:href="#importantSign" /></svg:svg></div>
<?php endif; ?>
			<?php echo $msg; ?>
		</p>
<?php endif; ?>
		<h3>Stack Trace</h3>
		<ol>
<?php
$i = 0;
$highlights = array();
foreach($fixedTrace as $trace):
	$i++;
	if(isset($trace['file']) && !isset($highlights[$trace['file']])) {
		$highlights[$trace['file']] = highlight_string(str_replace('	', '  ', file_get_contents($trace['file'])), true);
		$highlights[$trace['file']] = str_replace(array("\r\n", "\n", "\r"), array('', '', ''), $highlights[$trace['file']]);
		$highlights[$trace['file']] = str_replace(array('<code><span style="color: #000000">', '</span></code>', '&nbsp;'), array('', '', '&#160;'), $highlights[$trace['file']]);
		$highlights[$trace['file']] = explode('<br />', $highlights[$trace['file']]);
	}
?>
			<li id="frame<?php echo $i; ?>"<?php if($i != 2): ?> class="hidecode"<?php endif; ?>>at <?php if($i > 1): ?><strong><?php if(isset($trace['class'])): ?><?php echo $trace['class'], htmlspecialchars($trace['type']); ?><?php endif; ?><?php echo $trace['function']; ?><?php if(isset($trace['args'])): ?>(<?php echo buildParamList($trace['args']); ?>)<?php endif; ?></strong><?php else: ?><em>exception origin</em><?php endif; ?><br />in <?php if(isset($trace['file'])): echo str_replace(
			array(
				'_' . APPLICATION_PATH,
			),
			array(
				'<abbr title="' . APPLICATION_PATH . '">core.app_dir</abbr>'
			),
'' . $trace['file']); ?>
 <a href="#frame<?php echo $i; ?>" class="toggle" title="Toggle source code snippet" onclick="this.parentNode.className = this.parentNode.className == 'hidecode' ? '' : 'hidecode'; return false;">line <?php echo $trace['line']; ?></a><ol start="<?php echo $start = $trace['line'] < 4 ? 1 : $trace['line'] - 3; ?>" style="padding-left:<?php echo strlen($start+6)*0.6+2; ?>em"><?php
$lines = array_slice($highlights[$trace['file']], $start - 1, 7, true);
foreach($lines as $key => &$line) {
	if($key + 1 == $trace['line']): ?><li class="highlight"><?php if($svg): ?><div style="float:left; width:1em; height:1em; margin-left:-1.35em; background-color:#FFF;"><svg:svg viewBox="2 1 45 43" preserveAspectRatio="xMaxYMax meet" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><svg:use xlink:href="#stopSign" /></svg:svg></div><?php endif; else: ?><li><?php endif; ?><code><?php
	if($line == '') {
		$line = '&#160;';
	}
	if(strpos($line, '</span>') === 0) {
		$line = substr($line, 7);
	}
	if(strpos($line, '</span>') < strpos($line, '<span') || strpos($line, '<span') === false) {
		for($j = $key; $j >= 0; $j--) {
			if(($pos = strrpos($highlights[$trace['file']][$j], '<span')) !== false && strrpos($highlights[$trace['file']][$j], '</span>') < $pos) {
				$line = substr($highlights[$trace['file']][$j], $pos, 29) . $line; break;
			}
		}
	}
	if((strrpos($line, '</span>') < strrpos($line, '<span') || strpos($line, '</span>') === false) && strpos($line, '<span') !== false) {
		$line .= '</span>';
	}
	// Whoever figures out what the point of this is gets a free Agavi t-shirt shipped right to his doorstep.
	// It shall be left here, commented out, to serve as a reminder for any programmer to comment their code properly.
	// http://trac.agavi.org/ticket/1009 is the associated ticket, patiently waiting for an explanation.
	// if(strpos($line, ' ', 20) == 29) {
	// 	$line = substr_replace($line, '&#160;', 29, 1);
	// }
	echo $line;
?></code></li>
<?php } ?></ol><?php else: // no info about origin file ?><em>unknown</em><?php endif; ?></li>
<?php endforeach; ?>
		</ol>
<?php
$requestParams = $this->request->getParams();
if(!empty($requestParams)): ?>
		<h3>Request Params</h3>
		<pre>
<?php echo htmlspecialchars(print_r($requestParams, true)); ?>
		</pre>
<?php endif; ?>
		<h3>Version Information</h3>
		<dl>
			<dt>Zend Framework:</dt>
			<dd><?php echo Zend_Version::VERSION; ?></dd>
			<dt>PHP:</dt>
			<dd><?php echo htmlspecialchars(phpversion()); ?></dd>
			<dt>System:</dt>
			<dd><?php echo htmlspecialchars(php_uname()); ?></dd>
			<dt>Timestamp:</dt>
			<dd><?php echo gmdate(DATE_ISO8601); ?></dd>
		</dl>
	</body>
</html>